<?php
/**
 * Object Relational Persistence Mapping Class for table: lab_result
 * @package modules.dept_laboratory.ordo
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class LabResult extends ORDataObject {

	/**#@+
	 * Fields of table: lab_result mapped to class members
	 */
	var $lab_result_id		= '';
	var $lab_assigned_item_id		= '';	
	var $vi_name      = '';
	var $en_name      = '';
	var $nameText      = '';
	var $result		= '';
	var $normal_result_male		= '';
	var $normal_result_female		= '';
	var $enum_data_type		= '';
	var $enum_unit		= '';	
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'lab_result';

	/**
	 * Primary Key
	 */
	var $_key = 'lab_result_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'LabResult';

	/**
	 * Handle instantiation
	 */
	function __construct() {
		parent::ORDataObject();
	}
	
	public function createResultStructure($labAssignedItemId, $serviceId) {
		$currentLocale =   L10nHelper::getLocale();
		
		$nameField = '';
		$locales = L10nHelper::getSupportedLocales();
		foreach ($locales as $lo=>$la) {
		    $nameField .= L10nHelper::changeField($currentLocale, 'name') . ', ';
		}
		$sql = "INSERT INTO
			      $this->_table (
				      lab_assigned_item_id,
				      $nameField
				      normal_result_male,
				      normal_result_female,
				      enum_data_type,
				      enum_unit
				      )
			      SELECT $labAssignedItemId,
				      $nameField
				      normal_result_male,
				      normal_result_female,
				      enum_data_type,
				      enum_unit
			      FROM lab_item_factor
			      WHERE service_id='$serviceId' ";
		      
		$this->_execute($sql);      
		
	}
	
	public function deleleItems($labAssignedItemsId) {
		if (!is_array($labAssignedItemsId)) {
			return;
		}
		$condition = 'IN(';
		foreach ($labAssignedItemsId as $itemId) {
			$condition .= $itemId['lab_assigned_item_id'] . ',';
		}
		$condition .= '0)';
		$sql = "DELETE FROM $this->_table WHERE lab_assigned_item_id " . $condition;
		$this->_execute($sql);
		
	}
	
    public function listLabResultItems($labAssignedItemId) {
        $field    =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        
        $ds =& new Datasource_sql();
        $sql = array(
                'cols'  => "lab_result_id, t.$field name, result,normal_result_male, normal_result_female,
                            enum_data_type, enum_unit",
                'from'  => "$this->_table t",                                   
                'where' =>   "lab_assigned_item_id ='$labAssignedItemId'"            
                );
        $ds->setup($this->_db, $sql);
        return $ds->toArray();        
    }
    
    
    public function setResult($result) {
    	$this->result = $result;
    }

	
}
?>
